package com.example.kaiqiang.dagger2demo

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_android.view.*
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Inject


private const val ARG_PARAM1 = "type"
private const val ARG_PARAM2 = "name"


class MyFragment : Fragment() {

    private var param1: Int? = null
    private var param2: String? = null
    @Inject
    lateinit var api: MyApi.Api

    lateinit var androidAdapter: AndroidAdapter

    private
    lateinit var myAdapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getInt(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_android, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        DaggerFragmentComponent.builder().apiModule(ApiModule(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))).build().inject(this)

        view.sw.setOnRefreshListener { getData() }

        when (param1) {
            0 -> {
                view.rv.layoutManager = GridLayoutManager(context, 2)
                myAdapter = MyAdapter(null, context)
                view.rv.adapter = myAdapter
            }
            1 ,2-> {
                view.rv.layoutManager = LinearLayoutManager(context)
                androidAdapter = AndroidAdapter(context, null)
                view.rv.adapter = androidAdapter
                androidAdapter.setOnItemClickListener {
                    val intent = Intent(context, WebViewActivity::class.java)
                    intent.putExtra("title",it.desc)
                    intent.putExtra("url",it.url)
                    startActivity(intent)

                }
            }

        }


        //手动调用,通知系统去测量
        view.sw.measure(0, 0)
        view.sw.isRefreshing = true
        getData()
    }


    private fun getData() {
        when (param1) {
            0 -> api.getGirlData(10).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe { res ->
                view!!.sw.isRefreshing = false
                myAdapter.upData(res.results)
            }
            1 -> {
                api.getAndroidData(10).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe {
                    view!!.sw.isRefreshing = false
                    androidAdapter.upData(it.results)
                }
            }
            2 -> {
                api.getHData(10).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe {
                    view!!.sw.isRefreshing = false
                    androidAdapter.upData(it.results)
                }
            }
        }

    }


    companion object {

        @JvmStatic
        fun newInstance(param1: Int, param2: String) =
                MyFragment().apply {
                    arguments = Bundle().apply {
                        putInt(ARG_PARAM1, param1)
                        putString(ARG_PARAM2, param2)
                    }
                }
    }
}
